import dtlCtrl from './detail/detail.ctrl.js';
import dtlView from './detail/detail.html';
import delTmp from './form/delete.html';
import delCtrl from './form/delete.ctrl.js';

class Alarm {
    constructor(clusters, list, alarmSrv, $uiModal, ctgNotification,$timeout,$state) {
        "ngInject";
        this.alarmSrv      = alarmSrv;
        this.uiModal       = $uiModal;
        this._notification = ctgNotification;
        this._state=$state;
        this.clusters      = clusters;

        this.clusterInst   = clusters && clusters[0];
        this.optsTypes     = {'lt':'<','gt':'>','lte':'<=','gte':'>=','rt':'范围','rte':'范围等于'};

        this.page = {
            currentPage: 1,
            totalItems: 0
        };

        if (list) {
            angular.forEach(list.alertRuleObj, (ele)=>{
                angular.forEach(ele.ruleItem,(rule,index)=>{
                    if(rule.opts!='rt' && rule.opts!='rte'){
                        ele.ruleItem[index] = rule.typeDescription+'('+rule.unit+')'+this.optsTypes[rule.opts]+rule.min
                    }else{
                        ele.ruleItem[index] = rule.typeDescription+'('+rule.unit+')'+this.optsTypes[rule.opts]+rule.min+'~'+rule.max
                    }
                });
                angular.forEach(ele.service,(ser,index)=>{
                    ele.service[index] = ser.serviceName
                })
            });
            this.table = list.alertRuleObj;
            this.page.totalItems = list.num;
        } else this.table = [];
    }


    // 获取告警信息列表
    getList() {
        this.table  = null;
        this.loading = true;
        this.alarmSrv.getAlertRules({currentPage: this.page.currentPage},{clusterCode:this.clusterInst.clusterCode}).then(data=> {
            this.loading = false;
            angular.forEach(data.alertRuleObj, (ele)=>{
                angular.forEach(ele.ruleItem,(rule,index)=>{
                    if(rule.opts!='rt' && rule.opts!='rte'){
                        ele.ruleItem[index] = rule.typeDescription+'('+rule.unit+')'+this.optsTypes[rule.opts]+rule.min
                    }else{
                        ele.ruleItem[index] = rule.typeDescription+'('+rule.unit+')'+this.optsTypes[rule.opts]+rule.min+'~'+rule.max
                    }
                });
                angular.forEach(ele.service,(ser,index)=>{
                    ele.service[index] = ser.serviceName
                })
            });
            this.table = data.alertRuleObj;
            this.page.totalItems = data.num;

        }).catch(()=> {
            this.loading = false;
            this.table = [];
            this.page.totalItems = 0;
            this._notification.error('获取告警列表数据有误.');
        });
    }

    search() {
        this.page.currentPage = 1;
        this.getList();
    }

    deleteAlarm(ele){
        this.deleteFilter = {
            alertRuleDTO : {
                id:ele.id
            },
            clusterCode:this.clusterInst.clusterCode
        };
        this.uiModal.open({
            templateUrl: delTmp,
            controller: delCtrl,
            controllerAs: 'vm',
            windowClass: 'alert-dialog alert-dialog-ctg',
            resolve: {
                alert: function () {
                    return {
                        name: ele.alertName
                    }
                }
            }
        }).result.then(()=> {
                ele.deleting = true;
                this.alarmSrv.deleteAlertRule(this.deleteFilter).then(data=> {
                    this._notification.success('删除告警 ' + ele.alertName + ' 成功.');
                    this.getList();
                }).catch(data=> {
                    this._notification.error('删除告警' + ele.alertName + ' 失败.');
                }).finally(()=> {
                    ele.deleting = false;
                })
            });
    }
    addRules(){
        if(this.clusters&&this.clusters.length){
            this._state.go('alarm-k8s-pre-detail');
        }else{
            this._notification.error('请先配置集群.')
        }
    }
}
export default Alarm